package com.akcome.apollo.common;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

/**
 * 获取当前用户信息
 * @author wn
 *
 */
public class AuthUtils {

	public static SecurityUser getUser() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (authentication != null && authentication.getPrincipal() instanceof  SecurityUser) {
            SecurityUser userDetails = (SecurityUser) authentication.getPrincipal();
            if (userDetails != null) {
                return userDetails;
            }
        }
        return null;
    }

    /**
     * 获得用户ID
     *
     * @return
     */
    public static Long getUserId() {
    	SecurityUser user = AuthUtils.getUser();
        if (user != null) {
            return user.getId();
        }
        return null;
    }

    /**
     * 获得用户名
     *
     * @return
     */
    public static String getUserName() {
    	SecurityUser user =  AuthUtils.getUser();
        if (user != null) {
            return user.getUsername();
        }
        return null;
    }
    
    /**
     * 获得手机号
     *
     * @return
     */
    public static String getMobile() {
    	SecurityUser user =  AuthUtils.getUser();
        if (user != null) {
            return user.getMobile();
        }
        return null;
    }
}
